package com.qd.panda.sms;

import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.exception.ApiException;
import com.qd.system.sms.SmsService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 公众号/APP_短信
 * @author sjk
 */
@RestController
@RequestMapping("/sys/sms/")
@RequiredArgsConstructor
public class SmsController {
    private final SmsService smsService;

    /**
     * 发送验证码
     *
     * @param map 参数
     * @return ID
     */
    @RequestMapping(value = "/sendVerificationCode", method = RequestMethod.POST)
    public String sendVerificationCode(@RequestBody Map<String, Object> map) {
        final Object mobile = map.get("mobile");
        return smsService.sendVerificationCode(mobile);
    }

    /**
     * 验证码是否正确
     *
     * @param map 参数
     * @return true正确 false错误
     */
    @RequestMapping(value = "/verificationCode", method = RequestMethod.POST)
    public ApiResultWrapper<Object> verificationCode(@RequestBody Map<String, String> map) {
        final String id = map.get("verCodeId");
        final String vCode = map.get("verCode");
        final String mobile = map.get("mobile");
        if (!StringUtils.hasLength(id)) {
            throw new ApiException("验证码Id不能为空");
        }
        if (!StringUtils.hasLength(vCode)) {
            throw new ApiException("验证码不能为空");
        }
        return ApiResult.isSuccess(smsService.checkVerificationCode(mobile,id, vCode));
    }
}